home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / dbleQRP.cc < prev    next >
C/C++ Source or Header  |  1997-03-07  |  3KB  |  149 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include <cassert>
  32.  
  33. #include "dbleQRP.h"
  34. #include "f77-fcn.h"
  35. #include "lo-error.h"
  36. #include "mx-inlines.cc"
  37.  
  38. extern "C"
  39. {
  40.   int F77_FCN (dgeqpf, DGEQPF) (const int&, const int&, double*,
  41.                 const int&, int*, double*, double*,
  42.                 int&);
  43.  
  44.   int F77_FCN (dorgqr, DORGQR) (const int&, const int&, const int&,
  45.                 double*, const int&, double*, double*,
  46.                 const int&, int&); 
  47. }
  48.  
  49. // It would be best to share some of this code with QR class...
  50.  
  51. QRP::QRP (const Matrix& a, QR::type qr_type)
  52.   : QR (), p ()
  53. {
  54.   init (a, qr_type);
  55. }
  56.  
  57. void
  58. QRP::init (const Matrix& a, QR::type qr_type)
  59. {
  60.   assert (qr_type != QR::raw);
  61.  
  62.   int m = a.rows ();
  63.   int n = a.cols ();
  64.  
  65.   if (m == 0 || n == 0)
  66.     {
  67.       (*current_liboctave_error_handler) ("QR must have non-empty matrix");
  68.       return;
  69.     }
  70.  
  71.   Array<double> tau (m < n ? m : n);
  72.   double *ptau = tau.fortran_vec ();
  73.  
  74.   int lwork = 3*n > 32*m ? 3*n : 32*m;
  75.   Array<double> work (lwork);
  76.   double *pwork = work.fortran_vec ();
  77.  
  78.   int info = 0;
  79.  
  80.   Matrix A_fact = a;
  81.   if (m > n)
  82.     A_fact.resize (m, m, 0.0);
  83.  
  84.   double *tmp_data = A_fact.fortran_vec ();
  85.  
  86.   Array<int> jpvt (n, 0);
  87.   int *pjpvt = jpvt.fortran_vec ();
  88.  
  89.   // Code to enforce a certain permutation could go here...
  90.  
  91.   F77_XFCN (dgeqpf, DGEQPF, (m, n, tmp_data, m, pjpvt, ptau, pwork, info));
  92.  
  93.   if (f77_exception_encountered)
  94.     (*current_liboctave_error_handler) ("unrecoverable error in dgeqpf");
  95.   else
  96.     {
  97.       // Form Permutation matrix (if economy is requested, return the
  98.       // indices only!)
  99.  
  100.       if (qr_type == QR::economy)
  101.     {
  102.       p.resize (1, n, 0.0);
  103.       for (int j = 0; j < n; j++)
  104.         p.elem (0, j) = jpvt.elem (j);
  105.     }
  106.       else
  107.     {
  108.       p.resize (n, n, 0.0);
  109.       for (int j = 0; j < n; j++)
  110.         p.elem (jpvt.elem (j) - 1, j) = 1.0;
  111.     }
  112.  
  113.       if (qr_type == QR::economy && m > n)
  114.     r.resize (n, n, 0.0);
  115.       else
  116.     r.resize (m, n, 0.0);
  117.  
  118.       int min_mn = m < n ? m : n;
  119.  
  120.       for (int j = 0; j < n; j++)
  121.     {
  122.       int limit = j < min_mn-1 ? j : min_mn-1;
  123.       for (int i = 0; i <= limit; i++)
  124.         r.elem (i, j) = A_fact.elem (i, j);
  125.     }
  126.  
  127.       int n2 = m;
  128.       if (qr_type == QR::economy)
  129.     n2 = min_mn;
  130.  
  131.       F77_XFCN (dorgqr, DORGQR, (m, n2, min_mn, tmp_data, m, ptau,
  132.                  pwork, lwork, info));
  133.  
  134.       if (f77_exception_encountered)
  135.     (*current_liboctave_error_handler) ("unrecoverable error in dorgqr");
  136.       else
  137.     {
  138.       q = A_fact;
  139.       q.resize (m, n2);
  140.     }
  141.     }
  142. }
  143.  
  144. /*
  145. ;;; Local Variables: ***
  146. ;;; mode: C++ ***
  147. ;;; End: ***
  148. */
  149.